/**
 * @(#) TotalIncomProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				 연말정산 결과 조회 
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.ResultDataVO;
	import com.wcl.dataloader.DataLoaderFactory;
	import com.wcl.dataloader.IDataLoader;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class CalculationResultProxy extends CommonProxy
	{
		public static const NAME:String = "CalculationResultProxy";
		public var msgStr:String;
		public var resultList:ArrayCollection = new ArrayCollection();
		

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function CalculationResultProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		 //결과조회
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			loadData("Z_HR_0086_NEW", param, result, fault);
			
		}
		//시뮬레이션
		public function loadSimulation(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			loadData("Z_HR_0087_NEW", param, simulResult, simulFault);
			
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to load CalculationResult proxy");
				resultList = parseResultList(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_CULTRESULT_COMPLETE, this);
			}
			else {
				trace( "failed to load CalculationResult: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_CULTRESULT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in CalculationResult: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_CULTRESULT_FAILED, this);
		}
		
		public function simulResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to load CalculationResult proxy");
				resultList = parseResultList(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_SIMULRESULT_COMPLETE, this);
			}
			else {
				trace( "failed to load CalculationResult: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_SIMULRESULT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function simulFault(event:FaultEvent):void
		{
			trace("Fault message in CalculationResult: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_SIMULRESULT_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		private function parseResultList(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:ResultDataVO = new ResultDataVO();
				
				vo.ITMNO	 = StringUtil.trimFront(reg["ITMNO"],"0");
				vo.ITMNM	 = reg["ITMNM"];
				vo.LEDGER_H	 = reg["LEDGER_H"];
				vo.LEDGER_L	 = reg["LEDGER_L"];
				vo.ANZHL	 = reg["ANZHL"];
				vo.BETRG	 = reg["BETRG"];
				
				returnArr.addItem(vo);
			}
			return returnArr;
		}
		
	}
}